<?php
App::uses('AppController', 'Controller');
/**
 * Ordersproducts Controller
 *
 * @property Ordersproduct $Ordersproduct
 */
class OrdersproductsController extends AppController {

/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Ordersproduct->recursive = 0;
		$this->set('ordersproducts', $this->paginate(array('Ordersproduct.archive' => '0')));
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->Ordersproduct->exists($id)) {
			throw new NotFoundException(__('Invalid ordersproduct'));
		}
		$options = array('conditions' => array('Ordersproduct.' . $this->Ordersproduct->primaryKey => $id));
		$this->set('ordersproduct', $this->Ordersproduct->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->Ordersproduct->create();
			if ($this->Ordersproduct->save($this->request->data)) {
				$this->Session->setFlash(__('The ordersproduct has been saved'),'default', array('class' => 'savesuccess'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The ordersproduct could not be saved. Please, try again.'),'default', array('class' => 'saveerror'));
			}
		}
		$products = $this->Ordersproduct->Product->find('list');
		$orders = $this->Ordersproduct->Order->find('list');
		$this->set(compact('products', 'orders'));
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->Ordersproduct->exists($id)) {
			throw new NotFoundException(__('Invalid ordersproduct'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Ordersproduct->save($this->request->data)) {
				$this->Session->setFlash(__('The ordersproduct has been saved'),'default', array('class' => 'savesuccess'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The ordersproduct could not be saved. Please, try again.'),'default', array('class' => 'saveerror'));
			}
		} else {
			$options = array('conditions' => array('Ordersproduct.' . $this->Ordersproduct->primaryKey => $id));
			$this->request->data = $this->Ordersproduct->find('first', $options);
		}
		$products = $this->Ordersproduct->Product->find('list');
		$orders = $this->Ordersproduct->Order->find('list');
		$this->set(compact('products', 'orders'));
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @throws MethodNotAllowedException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$this->Ordersproduct->id = $id;
		if (!$this->Ordersproduct->exists()) {
			throw new NotFoundException(__('Invalid ordersproduct'));
		}
		$this->request->onlyAllow('post', 'delete');
		$data = array('id' => $id, 'archive' => '1');
		if ($this->Ordersproduct->save($data)) {

			//archive deliveriesordersproduct
			$this->loadModel('Deliveriesordersproduct');
			$dop = $this->Deliveriesordersproduct->find('list', array('conditions'=>array('ordersproducts_id'=>$id)));
			foreach ($dop as $j) {
				$data = array('id' => $j, 'archive' => '1');
				$this->Deliveriesordersproduct->save($data);
			}

			$this->Session->setFlash(__('Ordersproduct deleted'),'default', array('class' => 'savesuccess'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Ordersproduct was not deleted'),'default', array('class' => 'saveerror'));
		$this->redirect(array('action' => 'index'));
	}
}
